<template>
  <div class="page">
    <!-- <div class="top">
			<div style="float:left;"><span @click="goBack" class="goback">触发器管理</span>　|　<span>编辑触发器</span><span id="notice" style="margin-left:0.5rem;">注:页面所有修改，保存之后才可生效</span></div>
			<el-button type="primary" size="small" id="goback" @click="goBack">返回</el-button>
		</div> -->
    <span id="notice" style="margin-left:0.5rem;">注:页面所有修改，保存之后才可生效</span>
    <el-form :model="form" :inline="true" id="addform" class="demo-form-inline">
      <div class="line">
        <el-form-item label="项目名称:" :label-width="formLabelWidth2">
          <el-select v-model="form.product_id" placeholder="选择项目" class="inpu" @change="handleChange(form.product_id)">
            <el-option v-for="item in products" :key="item.value" :value="item.value" :label="item.label"
              :disabled="item.disabled">
            </el-option>
          </el-select>
          <!-- <el-cascader
						class="inpu"
						v-model="form.product_id"
						:options="products"
						:props="{checkStrictly:true,expandTrigger:'hover',emitPath:false}"
						@change="handleChange(form.product_id)">
					</el-cascader> -->
        </el-form-item>
        <el-form-item label="设备名称:" :label-width="formLabelWidth">
          <el-select v-model="form.equip_id" placeholder="选择设备" class="inpu" @change="changeDevice(form.equip_id)">
            <el-option v-for="item in devices" :key="item.value" :value="item.value" :label="item.label">
            </el-option>
          </el-select>
          <el-tooltip placement="top" effect="light">
            <div slot="content" style="line-height:0.4rem;color:red;">如果是全部，则默认应用于整个项目，数据点地址需要手动输入;全部时，不支持联动和数据转发</div>
            <span class="sortIcon" style="color:red;">?</span>
          </el-tooltip>
          <!-- <div class="tip" style="width:auto;height:0.4rem;">如果是全部，则默认应用于整个项目，地址需要手动输入</div> -->
        </el-form-item>
        <span class="leftName">设备选择</span>
      </div>
      <div class="line" v-loading.fullscreen="loading" element-loading-text="正在获取中..."
        element-loading-spinner="el-icon-loading">
        <el-form-item label="触发类型:" :label-width="formLabelWidth2">
          <el-select v-model="form.trigger_type" placeholder="选择类型" class="inpu"
            @change="changeTriggerType(form.trigger_type)">
            <el-option v-for="item in triggerTypes" :key="item.value" :value="item.value"
              :label="item.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.trigger_type==2" label="触发条件:" :label-width="formLabelWidth">
          <el-select v-model="form.condition" placeholder="选择条件" class="inpu" @change="changeCondition(form.condition)">
            <el-option v-for="item in whileIfs2" :key="item.value" :value="item.value" :label="item.label"></el-option>
          </el-select>
          <el-tooltip v-if="form.condition==4" placement="top" effect="light">
            <div slot="content" style="line-height:0.4rem;color:red;">注：断电触发需要相关硬件的支持才可以</div>
            <span class="sortIcon" style="color:red;">!</span>
          </el-tooltip>
        </el-form-item>
        <el-form-item v-if="showType && form.trigger_type==1" label="子设备名称:" :label-width="formLabelWidth">
          <el-select v-model="form.slave_id" placeholder="选择子设备" class="inpu" @change="changeSlave(form.slave_id)">
            <el-option v-for="item in slaves" :key="item.value" :value="item.value" :label="item.label">
            </el-option>
          </el-select>
          <el-tooltip placement="top" effect="light">
            <div slot="content" style="line-height:0.4rem;color:red;">如果是全部，地址需要手动输入</div>
            <span class="sortIcon" style="color:red;">?</span>
          </el-tooltip>
          <!-- <div class="tip" style="width:auto;height:0.4rem;">如果是全部，地址需要手动输入</div> -->
        </el-form-item>
        <el-form-item v-if="((!showType && form.equip_id==0) || (showType && form.slave_id==0)) && form.trigger_type==1"
          label="数据点地址:" :label-width="formLabelWidth">
          <el-input v-model="form.data_key" class="inpu" placeholder="请输入数据点地址"></el-input>
          <el-tooltip placement="top" effect="light">
            <div slot="content" style="line-height:0.4rem;color:red;">手动输入多个地址，请使用“,”(英文逗号)隔开，多个意为同时添加多个触发器</div>
            <span class="sortIcon" style="color:red;">?</span>
          </el-tooltip>
          <!-- <div class="tip" style="width:auto;height:0.4rem;">手动输入多个地址，请使用“,”(英文逗号)隔开，多个意为同时添加多个触发器</div> -->
        </el-form-item>
        <el-form-item v-if="((!showType && form.equip_id!=0) || (showType && form.slave_id!=0)) && form.trigger_type==1"
          label="数据点地址:" :label-width="formLabelWidth">
          <el-select v-model="form.data_key" placeholder="选择地址" class="inpu">
            <el-option v-for="item in dataKeys" :key="item.value" :value="item.value" :label="item.label">
            </el-option>
          </el-select>
        </el-form-item><br>
        <el-form-item v-if="form.trigger_type==1" label="触发条件:" :label-width="formLabelWidth2">
          <el-select v-model="form.condition" placeholder="选择条件" class="inpu">
            <el-option v-for="item in whileIfs" :key="item.value" :value="item.value" :label="item.label"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item v-if="form.condition!=6 && form.condition!=7 && form.trigger_type==1" label="　"
          label-width="64px">
          值A：<el-input v-model="form.value1" class="inpu" style="width:1.85rem" placeholder="值A"></el-input>
          值B：<el-input :disabled="form.condition!=4 && form.condition!=5" v-model="form.value2" style="width:1.85rem"
            class="inpu" placeholder="值B"></el-input>
        </el-form-item>
        <el-form-item v-if="form.trigger_type==1" label="保持时间:" label-width="124px">
          <el-input v-model="form.keep_time" auto-complete="off" class="inpu" placeholder="请输入数据点地址"></el-input> 秒
          <el-tooltip placement="top" effect="light" style="margin-left:0.1rem;margin-top:-2px;">
            <div slot="content" style="line-height:0.4rem;color:red;">只有持续触发报警超过该时间才会推送报警信息，否则不推送，0为立即推送</div>
            <span class="sortIcon" style="color:red;">?</span>
          </el-tooltip>
          <!-- <div class="tip" style="width:auto;height:0.4rem;">只有持续触发报警超过该时间才会推送报警信息，否则不推送，0为立即推送</div> -->
        </el-form-item>
        <span class="leftName">条件设置</span>
      </div>
      <div class="line" style="padding-top:0.5rem;">
        <el-form-item label="设备报警:" :label-width="formLabelWidth2" style="margin-top:-0.2rem;">
          <el-switch v-model="form.sync_alarm" :width="stateWidth" class="demo"
            style="display:inline-block;vertical-align:top;" active-color="#13ce66" inactive-color="#ccc"
            active-text="启用" inactive-text="禁用" :active-value="one"
            :disabled="form.trigger_type==2 && form.condition==5" @change="changeAlarm(form.sync_alarm)"
            :inactive-value="zero">
          </el-switch>
        </el-form-item>
        <el-form-item label="报警推送:" :label-width="win.screen.width>1280?formLabelWidth:'80px'"
          style="margin-top:-0.2rem;margin-left:-1.6rem;">
          <el-switch v-model="form.is_send" :width="stateWidth" class="demo"
            style="display:inline-block;vertical-align:top;" active-color="#13ce66" inactive-color="#ccc"
            active-text="启用" inactive-text="禁用" :active-value="one" :disabled="!form.sync_alarm"
            @change="changeSend(form.is_send)" :inactive-value="zero">
          </el-switch>
        </el-form-item>
        <el-form-item label="恢复报警:" :label-width="win.screen.width>1280?formLabelWidth:'80px'"
          style="margin-top:-0.2rem;margin-left:-1.6rem;margin-bottom:10px;">
          <el-switch v-model="form.recover_alarm" :width="stateWidth" class="demo"
            style="display:inline-block;vertical-align:top;" active-color="#13ce66" inactive-color="#ccc"
            active-text="启用" inactive-text="禁用" :active-value="one"
            :disabled="!form.is_send || !form.sync_alarm || form.trigger_type==2" :inactive-value="zero">
          </el-switch>
        </el-form-item>
        <el-form-item label="" label-width="20" style="margin-top:-0.2rem;margin-bottom:10px;margin-left:-1rem;">
          <el-button size="mini" style="font-size:0.3rem;" @click="dialogAlarmVisible=true">报警说明</el-button>
        </el-form-item><br>
        <div v-if="form.is_send==1">
          <!-- <el-form-item label="微信报警:" :label-width="formLabelWidth2" style="margin-top:-0.2rem;">
				<span class="typeBox" :class="form.wx_alarm==1?'isChoose':'onChoose'"   @click="chooseAlarm('wx',form.wx_alarm)">√</span>
				<span style="margin-left:0.7rem;">需要有开启接收权限的平台账号，并绑定公众号，不产生费用</span>
			</el-form-item><br> -->
          <el-form-item label="企业微信报警:" :label-width="formLabelWidth2" style="margin-top:-0.2rem;">
            <span class="typeBox" :class="form.qy_alarm==1?'isChoose':'onChoose'"
              @click="chooseAlarm('qy',form.qy_alarm)">√</span>
            <!-- <span style="margin-left:0.7rem;">需要有开启接收权限的平台账号，并绑定公众号，不产生费用</span> -->
          </el-form-item>
          <el-form-item label="机器人URL:" :label-width="formLabelWidth" style="margin-top:-0.2rem;">
            <el-input class="inpu" style="width:10rem;margin-top:0.1rem;" v-model="form.qy_addr"
              placeholder="请输入企业微信的机器人url" type="textarea" :rows="1"></el-input>
          </el-form-item><br>
          <!-- <el-form-item label="短信报警:"  :label-width="formLabelWidth2" style="margin-top:-0.2rem;">
				<span class="typeBox" :class="form.sms_alarm==1?'isChoose':'onChoose'"  @click="chooseAlarm('sms',form.sms_alarm)">√</span>

			</el-form-item>
			<el-form-item label="联络人:" :label-width="formLabelWidth" style="margin-top:-0.2rem;">
				<el-input class="inpu"  style="width:10rem;margin-top:0.1rem;" v-model="form.sms_contact" placeholder="请输入联络人的手机号,多个用英文逗号','间隔"  type="textarea" :rows="1" ></el-input>
			</el-form-item><br>
			<el-form-item label="语音报警:" :label-width="formLabelWidth2" style="margin-top:-0.15rem;">
				<span class="typeBox" :class="form.voice_alarm==1?'isChoose':'onChoose'"   @click="chooseAlarm('voice',form.voice_alarm)">√</span>

			</el-form-item>
			<el-form-item label="联络人:" :label-width="formLabelWidth" style="margin-top:-0.15rem;">
				<el-input class="inpu"  style="width:10rem;margin-top:0.1rem;" v-model="form.voice_contact" placeholder="请输入联络人的手机号,多个用英文逗号','间隔"  type="textarea" :rows="1" ></el-input>
			</el-form-item><br> -->

          <el-form-item label="告警内容:" :label-width="formLabelWidth2" style="margin-top:-0.05rem;">
            <el-input class="inpu" style="width:12.92rem;" v-model="form.alarm_content" placeholder="有疑问可参考报警说明"
              autosize type="textarea"></el-input>
          </el-form-item><br>

          <div v-if="form.recover_alarm==1">
            <el-form-item label="恢复告警内容:" :label-width="formLabelWidth2" style="margin-top:-0.14rem;">
              <el-input class="inpu" style="width:12.92rem;" v-model="form.recover_content" placeholder="有疑问可参考报警说明"
                autosize type="textarea"></el-input>
            </el-form-item>
          </div>
          <!-- <el-form-item label="　　　　　" v-if="form.voice_alarm==1 || form.sms_alarm==1" :label-width="formLabelWidth2"> -->
          <!-- <p style="line-height: 0.4rem;">报警内容说明：报警提示的完整模板为："您好，您的设备(具体的设备名称)有新的告警，告警内容:(具体的告警内容)，告警时间:(具体的告警时间)。请尽快查看处理!"，配置时只需要将具体的告警内容填入输入框，其他内容不用填写，也不可改变！设备名称和告警时间会自动获取，无需配置！</p> -->
          <!-- </el-form-item> -->
          <el-form-item label="付款账号:" v-if="form.voice_alarm==1 || form.sms_alarm==1" :label-width="formLabelWidth2"
            style="margin-top:-0.05rem;">
            <el-select v-model="form.payer_id" class="inpu">
              <el-option class="oneOption" v-for="item in childrenFinance" :key="item.value" :value="item.value"
                :label="item.label" :style="{'text-align':'left','padding-left':'0.3rem'}">
              </el-option>
            </el-select>
          </el-form-item>
        </div>
        <span class="leftName">报警配置</span>
      </div>
      <div class="line" style="padding-top:0.5rem;padding-bottom:0.2rem;">
        <el-form-item label="设备联动:" :label-width="win.screen.width>1280?formLabelWidth:'80px'"
          style="margin-bottom:2px;margin-left:1.2rem;">
          <el-switch v-model="form.is_linkage" :width="stateWidth" class="demo"
            style="display:inline-block;vertical-align:top;" active-color="#13ce66" inactive-color="#ccc"
            active-text="启用" inactive-text="禁用" :active-value="one"
            :disabled="(form.trigger_type==2 && form.condition==5) || form.equip_id==0" :inactive-value="zero">
          </el-switch>
        </el-form-item>
        <el-form-item v-if="form.is_linkage==1 && form.trigger_type==2  && form.equip_id!=0" label="联动保留:"
          :label-width="win.screen.width>1280?formLabelWidth:'80px'" style="margin-bottom:2px;margin-left: -1.6rem;">
          <el-switch v-model="form.is_retain" :width="stateWidth" class="demo"
            style="display:inline-block;vertical-align:top;" active-color="#13ce66" inactive-color="#ccc"
            active-text="启用" inactive-text="禁用" :active-value="one" :inactive-value="zero">
          </el-switch>
        </el-form-item>
        <el-form-item v-if="form.is_linkage==1" label="　　　" :label-width="win.screen.width>1280?formLabelWidth:'80px'"
          style="margin-bottom:2px;margin-left: -2.6rem;">
          <el-button size="mini" class="add" @click="preAddLink">添加</el-button>
        </el-form-item>
        <el-form-item label="　　　" :label-width="win.screen.width>1280?formLabelWidth:'80px'"
          style="margin-bottom:2px;margin-left:-1.8rem;">
          <el-button size="mini" style="font-size:0.3rem;" @click="dialogLinkageVisible=true">联动说明</el-button>
        </el-form-item><br>
        <el-table v-if="form.is_linkage==1" :data="tableData" size="mini" :header-cell-style="{'fontSize':'0.3rem'}"
          :cell-style="{'fontSize':'0.3rem'}" style="width:90%;margin-left:80px;text-align:center;">
          <el-table-column prop="equip_name" label="设备名称" min-width="160">
          </el-table-column>
          <el-table-column prop="slave_name" label="子设备名称" min-width="150">
          </el-table-column>
          <el-table-column prop="sensor_name" label="数据点地址" min-width="150">
          </el-table-column>
          <el-table-column v-if="form.trigger_type==1" prop="linkage_value" label="触发下发值" min-width="100">
          </el-table-column>
          <el-table-column v-if="form.trigger_type==1" prop="linkage_recover_value" label="恢复下发值" min-width="100">
          </el-table-column>
          <el-table-column v-if="form.trigger_type==2 && (form.condition==1 || form.condition==2)" prop="linkage_value"
            label="上线下发值" min-width="100">
          </el-table-column>
          <el-table-column v-if="form.trigger_type==2 && form.condition==3" prop="linkage_value" label="下线下发值"
            min-width="100">
          </el-table-column>
          <el-table-column v-if="form.trigger_type==2 && form.condition==4" prop="linkage_value" label="断电下发值"
            min-width="100">
          </el-table-column>
          <el-table-column v-if="form.trigger_type==2 && form.condition==1" prop="linkage_recover_value" label="下线下发值"
            min-width="100">
          </el-table-column>
          <el-table-column label="操作" min-width="120" align="center">
            <template slot-scope="scope">
              <el-button @click="linkEdit(scope.row,scope.$index)" type="text" style="color:#409Eff;font-size:0.3rem;"
                size="mini">编辑</el-button>
              <el-button @click="sureDelete(scope.row)" type="text" size="mini"
                style="color:red;font-size:0.3rem">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <span class="leftName">联动配置</span>
      </div>
      <div class="line" style="padding-top:0.5rem;padding-bottom:0.2rem;">
        <el-form-item label="数据转发:" :label-width="win.screen.width>1280?formLabelWidth:'80px'"
          style="margin-bottom:2px;margin-left:1.2rem;">
          <el-switch v-model="form.is_forward" :width="stateWidth" class="demo"
            style="display:inline-block;vertical-align:top;" active-color="#13ce66" inactive-color="#ccc"
            active-text="启用" inactive-text="禁用" :active-value="one"
            :disabled="(form.trigger_type==2 && form.condition!=5) || form.equip_id==0" :inactive-value="zero">
          </el-switch>
        </el-form-item>
        <el-form-item v-if="form.is_forward==1" label="　　　" :label-width="win.screen.width>1280?formLabelWidth:'80px'"
          style="margin-bottom:2px;margin-left: -2.6rem;">
          <el-button size="mini" class="add" @click="addForward">添加</el-button>
        </el-form-item>
        <el-form-item label="　　　" :label-width="win.screen.width>1280?formLabelWidth:'80px'"
          style="margin-bottom:2px;margin-left:-1.8rem;">
          <el-button size="mini" style="font-size:0.3rem;" @click="forwardInfo">转发说明</el-button>
        </el-form-item><br>
        <el-table v-if="form.is_forward==1" :data="form.forwards" size="mini" :header-cell-style="{'fontSize':'0.3rem'}"
          :cell-style="{'fontSize':'0.3rem'}" style="width:90%;margin-left:80px;text-align:center;">
          <el-table-column prop="forward_equip_name" label="设备名称" min-width="160">
          </el-table-column>
          <el-table-column prop="" label="转发模板" min-width="300">
            <template slot-scope="scope">
              <span>点击右侧编辑查看</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" min-width="120" align="center">
            <template slot-scope="scope">
              <el-button @click="forwardEdit(scope.row,scope.$index)" type="text"
                style="color:#409Eff;font-size:0.3rem;" size="mini">编辑</el-button>
              <el-button @click="sureDeleteForward(scope.row)" type="text" size="mini"
                style="color:red;font-size:0.3rem">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <span class="leftName">数据转发</span>
      </div>
      <el-form-item style="margin-left:0.8rem;margin-top:0.2rem;">
        <el-button type="success" size="small" @click="sureUpdate">保存</el-button>
      </el-form-item>
    </el-form>

    <el-dialog :visible.sync="addFormVisible" width="30%" custom-class="dlog" append-to-body :show-close="close"
      top="150px">
      <div class="toptit">
        <span>添加联动</span>
        <span style="float:right;cursor: pointer;" @click="closeAddDialog">关闭</span>
      </div>
      <el-form :model="aboutForm" block style="margin-top:0.4rem;text-align: left;">
        <el-form-item label="设备名称:" :label-width="formLabelWidth">
          <el-select v-model="aboutForm.linkage_equip_id" placeholder="选择设备" class="inpu"
            @change="changeOnDevice(aboutForm.linkage_equip_id)">
            <el-option v-for="item in onDevices" :key="item.value" :value="item.value" :label="item.label">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="子设备名称:" :label-width="formLabelWidth" v-if="aboutForm.type==1">
          <el-select v-model="aboutForm.linkage_slave_id" placeholder="选择子设备" class="inpu"
            @change="changeOnSlave(aboutForm.linkage_slave_id)">
            <el-option v-for="item in onSlaves" :key="item.value" :value="item.value" :label="item.label">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据点地址:" :label-width="formLabelWidth">
          <el-select v-model="aboutForm.linkage_sensor_id" placeholder="选择数据点,单选" class="inpu">
            <el-option v-for="item in onSensors" :key="item.value" :value="item.value" :label="item.label">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.trigger_type==1" label="触发下发值:" :label-width="formLabelWidth">
          <el-input v-model="aboutForm.linkage_value" class="inpu" style="width:2.25rem;"></el-input>
        </el-form-item>
        <el-form-item v-if="form.trigger_type==1" label="恢复下发值:" :label-width="formLabelWidth">
          <el-input v-model="aboutForm.linkage_recover_value" class="inpu" style="width:2.25rem;"></el-input>
          <span style="color:red;font-size:0.3rem;margin-left:0.1rem;"> 为空则不下发</span>
        </el-form-item>
        <el-form-item v-if="form.trigger_type==2 && (form.condition==1 || form.condition==2)" label="上线下发值:"
          :label-width="formLabelWidth">
          <el-input v-model="aboutForm.linkage_value" class="inpu" style="width:2.25rem;"></el-input>
        </el-form-item>
        <el-form-item v-if="form.trigger_type==2 && form.condition==3" label="下线下发值:" :label-width="formLabelWidth">
          <el-input v-model="aboutForm.linkage_value" class="inpu" style="width:2.25rem;"></el-input>
        </el-form-item>
        <el-form-item v-if="form.trigger_type==2 && form.condition==1" label="下线下发值:" :label-width="formLabelWidth">
          <el-input v-model="aboutForm.linkage_recover_value" class="inpu" style="width:2.25rem;"></el-input>
        </el-form-item>
        <el-form-item v-if="form.trigger_type==2 && form.condition==4" label="断电下发值:" :label-width="formLabelWidth">
          <el-input v-model="aboutForm.linkage_value" class="inpu" style="width:2.25rem;"></el-input>
        </el-form-item>

      </el-form>
      <div style="text-align:right;">
        <el-button size="small" @click="closeAddDialog">取 消</el-button>
        <el-button type="primary" size="small" @click="addLink">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="editFormVisible" width="30%" custom-class="dlog" append-to-body :show-close="close"
      top="150px">
      <div class="toptit">
        <span>修改联动</span>
        <span style="float:right;cursor: pointer;" @click="cancelEdit">关闭</span>
      </div>
      <el-form :model="editForm" block style="margin-top:0.4rem;text-align: left;">
        <el-form-item label="设备名称:" :label-width="formLabelWidth">
          <el-select v-model="editForm.linkage_equip_id" placeholder="选择设备" class="inpu"
            @change="changeEditDevice(editForm.linkage_equip_id)">
            <el-option v-for="item in onDevices" :key="item.value" :value="item.value" :label="item.label">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="子设备名称:" :label-width="formLabelWidth" v-if="editForm.type==1">
          <el-select v-model="editForm.linkage_slave_id" placeholder="选择子设备" class="inpu"
            @change="changeEditSlave(editForm.linkage_slave_id)">
            <el-option v-for="item in linkSlaves" :key="item.value" :value="item.value" :label="item.label">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据点地址:" :label-width="formLabelWidth">
          <el-select v-model="editForm.linkage_sensor_id" placeholder="选择数据点,单选" class="inpu">
            <el-option v-for="item in linkSensors" :key="item.value" :value="item.value" :label="item.label">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item v-if="form.trigger_type==1" label="触发下发值:" :label-width="formLabelWidth">
          <el-input v-model="editForm.linkage_value" class="inpu" style="width:2.25rem;"></el-input>
        </el-form-item>
        <el-form-item v-if="form.trigger_type==1" label="恢复下发值:" :label-width="formLabelWidth">
          <el-input v-model="editForm.linkage_recover_value" class="inpu" style="width:2.25rem;"></el-input>
          <span style="color:red;font-size:0.3rem;margin-left:0.1rem;"> 为空则不下发</span>
        </el-form-item>
        <el-form-item v-if="form.trigger_type==2 && (form.condition==1 || form.condition==2)" label="上线下发值:"
          :label-width="formLabelWidth">
          <el-input v-model="editForm.linkage_value" class="inpu" style="width:2.25rem;"></el-input>
        </el-form-item>
        <el-form-item v-if="form.trigger_type==2 && form.condition==3" label="下线下发值:" :label-width="formLabelWidth">
          <el-input v-model="editForm.linkage_value" class="inpu" style="width:2.25rem;"></el-input>
        </el-form-item>
        <el-form-item v-if="form.trigger_type==2 && form.condition==1" label="下线下发值:" :label-width="formLabelWidth">
          <el-input v-model="editForm.linkage_recover_value" class="inpu" style="width:2.25rem;"></el-input>
        </el-form-item>
        <el-form-item v-if="form.trigger_type==2 && form.condition==4" label="断电下发值:" :label-width="formLabelWidth">
          <el-input v-model="editForm.linkage_value" class="inpu" style="width:2.25rem;"></el-input>
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button size="small" @click="cancelEdit">取 消</el-button>
        <el-button type="primary" size="small" @click="sureEdit">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="addForwardVisible" width="55%" v-el-dialog-drag v-el-dialog-drag-width
      :close-on-click-modal="false" class="datadial" append-to-body :show-close="close" top="150px">
      <div class="toptit">
        <span>添加转发</span>
        <span style="float:right;cursor: pointer;" @click="cancelAddForward">关闭</span>
      </div>
      <el-form :model="addForwardForm" inline label-width="auto" style="margin-top:0.4rem;text-align: left;">
        <el-form-item label="接收设备:">
          <el-select v-model="addForwardForm.forward_equip_id" placeholder="选择设备" class="inpu"
            @change="changeForwardEquip(addForwardForm.forward_equip_id,'add')">
            <el-option v-for="item in forwardDevices" :key="item.value" :value="item.value" :label="item.label">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="small" @click="addDomain('add')">添加转发点</el-button>
          <!-- <el-button size="small" v-if="!drawerShow" @click="varStringHelp">打开公式生成器</el-button> -->
          <!-- <el-button size="small" v-else  @click="varStringHelp">关闭公式生成器</el-button> -->
          <el-button size="small" @click="formulaInfoVisible=true">计算公式说明</el-button>
        </el-form-item>
        <div class="sensorHead" ref="sensorHead">
          <span class="inpu" v-if="addForwardForm.equip_type==1">接收子设备</span>
          <span class="inpu">接收数据点</span>
          <span class="inpu" style="width:7.5rem;">源数据点及计算公式</span>
          <span class="inpu" style="width:1rem" v-if="addForwardForm.forward_template.length>1">操作</span>
        </div>
        <el-form-item v-for="(domain, index) in addForwardForm.forward_template" class="oneDomain"
          :prop="'forward_template.' + index + '.sensor_id'" :key="index">
          <el-select v-if="addForwardForm.equip_type==1" v-model="domain.slave_id" placeholder="子设备" class="inpu"
            @change="changeForwardSlave(domain.slave_id,index,'add')">
            <el-option v-for="item in forwardSlaves" :key="item.value" :value="item.value" :label="item.label">
            </el-option>
          </el-select>
          <el-select v-model="domain.data_key" placeholder="数据点" class="inpu">
            <el-option v-for="item in domain.sensors" :key="item.value" :value="item.value" :label="item.label">
            </el-option>
          </el-select>
          <el-input class="inpu over" style="width:7.5rem;" v-model="domain.formula" type="text" :title="domain.formula"
            placeholder="双击自动填入(或替换为)右侧计算公式" @dblclick.native="fillString(domain)"></el-input>
          <!-- <el-input class="inpu" style="width:6rem;" v-model="domain.formula" type="textarea"  :autosize="{ minRows: 1, maxRows: 4 }" placeholder="计算公式"></el-input> -->
          <el-button @click.prevent="removeDomain(domain,'add')" type="danger" size="mini"
            v-if="addForwardForm.forward_template.length>1">删除</el-button>
        </el-form-item>
        <!-- <el-form-item label="转发模板:" :label-width="formLabelWidth">
			<el-input v-model="addForwardForm.forward_template" class="inpu"  style="width:10rem;" type="textarea"  :rows="6"></el-input>
        </el-form-item> -->
      </el-form>
      <div style="text-align:right;margin-top:0.3rem;">
        <el-button size="small" @click="cancelAddForward">取 消</el-button>
        <el-button type="primary" size="small" @click="sureAddForward">确 定</el-button>
      </div>
      <FormulaCreater :drawerShow="drawerShow" :sensorTree="sensorTree" :getSensorString="getSensorString">
      </FormulaCreater>
    </el-dialog>
    <el-dialog :visible.sync="editForwardVisible" width="55%" v-el-dialog-drag v-el-dialog-drag-width
      :close-on-click-modal="false" class="datadial" append-to-body :show-close="close" top="150px">
      <div class="toptit">
        <span>编辑转发</span>
        <span style="float:right;cursor: pointer;" @click="cancelEditForward">关闭</span>
      </div>
      <el-form :model="editForwardForm" inline label-width="auto" v-loading="editForwardLoading"
        element-loading-text="正在获取中..." element-loading-spinner="el-icon-loading"
        style="margin-top:0.4rem;text-align: left;">
        <el-form-item label="接收设备:">
          <el-select v-model="editForwardForm.forward_equip_id" placeholder="选择设备" class="inpu"
            @change="changeForwardEquip(editForwardForm.forward_equip_id,'edit')">
            <el-option v-for="item in forwardDevices" :key="item.value" :value="item.value" :label="item.label">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="small" @click="addDomain('edit')">添加转发点</el-button>
          <!-- <el-button size="small" v-if="!drawerShow" @click="varStringHelp">打开公式生成器</el-button> -->
          <!-- <el-button size="small" v-else  @click="varStringHelp">关闭公式生成器</el-button> -->
          <el-button size="small" @click="formulaInfoVisible=true">源数据点及计算公式</el-button>
        </el-form-item>
        <div class="sensorHead" ref="sensorHead">
          <span class="inpu" v-if="editForwardForm.equip_type==1">接收子设备</span>
          <span class="inpu">接收数据点</span>
          <span class="inpu" style="width:7.5rem;">计算公式</span>
          <span class="inpu" style="width:1rem" v-if="editForwardForm.forward_template.length>1">操作</span>
        </div>
        <el-form-item v-for="(domain, index) in editForwardForm.forward_template" class="oneDomain"
          :prop="'forward_template.' + index + '.sensor_id'" :key="index">
          <el-select v-if="editForwardForm.equip_type==1" v-model="domain.slave_id" placeholder="子设备" class="inpu"
            @change="changeForwardSlave(domain.slave_id,index,'edit')">
            <el-option v-for="item in forwardSlaves" :key="item.value" :value="item.value" :label="item.label">
            </el-option>
          </el-select>
          <el-select v-model="domain.data_key" placeholder="数据点" class="inpu">
            <el-option v-for="item in domain.sensors" :key="item.value" :value="item.value" :label="item.label">
            </el-option>
          </el-select>
          <el-input class="inpu over" style="width:7.5rem;" :title="domain.formula" v-model="domain.formula" type="text"
            placeholder="双击自动填入(或替换为)右侧计算公式" @dblclick.native="fillString(domain)"></el-input>
          <!-- <el-input class="inpu" style="width:6rem;" v-model="domain.formula" type="textarea"  :autosize="{ minRows: 1, maxRows: 4 }" placeholder="计算公式"></el-input> -->
          <el-button @click.prevent="removeDomain(domain,'edit')" type="danger" size="mini"
            v-if="editForwardForm.forward_template.length>1">删除</el-button>
        </el-form-item>
        <!-- <el-form-item label="转发模板:" :label-width="formLabelWidth" >
			<el-input v-model="editForwardForm.forward_template" class="inpu" style="width:10rem;" type="textarea"  :rows="6"></el-input>
        </el-form-item> -->
      </el-form>
      <div style="text-align:right;margin-top:0.3rem;">
        <el-button size="small" @click="cancelEditForward">取 消</el-button>
        <el-button type="primary" size="small" @click="sureEditForward">确 定</el-button>
      </div>
      <FormulaCreater :drawerShow="drawerShow" :sensorTree="sensorTree" :getSensorString="getSensorString">
      </FormulaCreater>
    </el-dialog>
    <el-dialog :visible="formulaInfoVisible" :width="win.screen.width>=1600?'44%':'64%'" :show-close="false"
      append-to-body top="200px" class="dial">
      <div class="toptit">
        <span>计算公式说明</span>
        <span style="float:right;cursor: pointer;" @click="formulaInfoVisible=false">关闭</span>
      </div>
      <FormulaInfo></FormulaInfo>
    </el-dialog>
    <el-dialog :visible="dialogAlarmVisible" :width="win.screen.width>=1600?'42%':'62%'" :show-close="false"
      append-to-body top="200px" class="dial">
      <div class="toptit">
        <span>报警说明</span>
        <span style="float:right;cursor: pointer;" @click="dialogAlarmVisible=false">关闭</span>
      </div>
      <div class="infocontent" style="margin-top:0.2rem;">
        <p>
        <h4>开关说明：</h4>
        设备报警：当前触发器是否作为报警触发器使用；<br>
        报警推送：当数据点达到条件时,是否发送报警消息,只有作为报警触发时,此项才可使用；<br>
        恢复报警：当数据点恢复正常时,是否发送提示消息,只有开启报警推送后,此项才可使用；<br>
        <h4>告警内容说明：</h4>
        1、告警通知模板：尊敬的云控物联平台用户，您的设备“<设备名称>”有新的告警，告警内容：<告警内容>，请及时查看处理!(微信/短信/语音格式略有不同);<br>
            2、告警通知仅支持告警内容自定义，其他为固定格式不支持编辑;<br>
            3、告警内容支持的变量有： 分组名称：{groupName}，数据点名称：{sensorName}，报警条件：{condition}，报警值：{value};<br>
            4、告警内容为空则默认系统格式，系统格式：{groupName} {sensorName} {condition};<br>
            5、短信告警内容实际长度为1~35字符，超过将无法发送，不支持表情字符等特殊字符;
            <!-- <h4>报警计费规则：</h4>
		1、短信通知每条0.05元，长度70个字符以内按单条计费，长度超过70个字符按65个字符拆分成多条计费;<br>
		2、语音通知每分钟（约65个字符播放三次）0.1元，1分钟以内按1分钟计费; -->

            </p>
      </div>
    </el-dialog>
    <el-dialog :visible="dialogLinkageVisible" :width="win.screen.width>=1600?'42%':'62%'" :show-close="false"
      append-to-body top="200px" class="dial">
      <div class="toptit">
        <span>联动说明</span>
        <span style="float:right;cursor: pointer;" @click="dialogLinkageVisible=false">关闭</span>
      </div>
      <div class="infocontent" style="margin-top:0.2rem;">
        <p>
        <h4>开关说明：</h4>
        设备联动：当数据点达到条件时,是否触发其他(含其他设备)的数据点发生改变(写值),可与报警结合使用；<br>
        联动保留：触发类型为上下线触发,当被联动设备重新上线时,是否触发联动(只触发联动设备当前状态的对应事件)；
        <h4>注意事项：</h4>
        1.设备报警和设备联动及数据转发请至少启用一项,否则将失去意义；<br>
        2.添加或编辑联动时所绑定的数据点必须是读写变量，会自动屏蔽只读变量；<br>
        3.当触发器只作为设备联动触发时,请关闭设备报警项,否则可能会不停发送报警信息(或提示)；<br>
        4.当被联动设备的版本高于5.1时,联动保留才会生效；
        </p>
      </div>
    </el-dialog>
    <el-dialog :visible="forwardInfoVisible" :width="win.screen.width>=1600?'42%':'62%'" :show-close="false"
      append-to-body top="200px" class="dial">
      <div class="toptit">
        <span>转发说明</span>
        <span style="float:right;cursor: pointer;" @click="forwardInfoVisible=false">关闭</span>
      </div>
      <div class="infocontent" style="margin-top:0.2rem;">
        <!-- <h2 style="width:100%;text-align: center;margin-top:0.2rem;"></h2><br> -->
        <p>
          1.支持设备内部多个数据点进行简单四则运算，暂不提供函数计算；<br>
          2.转发只支持单设备的数据触发和数据上报事件触发；
        </p>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    whileIfs
  } from '@/common.js';
  import FormulaCreater from '../templates/FormulaCreater.vue';
  import FormulaInfo from '../templates/FormulaInfo.vue';
  export default {
    name: 'editHappen',
    data() {
      return {
        win: window,
        close: false,
        //grade: localStorage.getItem("grade"),
        user: [],
        one: 1,
        zero: 0,
        dialogAlarmVisible: false,
        dialogLinkageVisible: false,
        addForwardVisible: false,
        editForwardVisible: false,
        forwardInfoVisible: false,
        formulaInfoVisible: false,
        formLabelWidth2: '160px',
        formLabelWidth: '100px',
        value: '',
        devid: null,
        close: false,
        loading: true,
        drawerShow: false,
        filterText: '',
        sensorTree: [],
        sensorString: '',
        isCopy: false,
        forwardDevices: [],
        editForwardLoading: false,
        addFormVisible: false,
        editFormVisible: false,
        showType: false,
        editIndex: 0,
        editForwardIndex: 0,
        dataKeys: [],
        onSensors: [],
        stateWidth: 60,
        baseurl: this.$baseurl,
        id: 0,
        showdevice: false,
        option: '',
        form: {
          id: 0,
          condition: '',
          linkages: [],
          is_linkage: 0,
          is_retain: 0,
          is_send: 0,
          sync_alarm: 0,
          recover_alarm: 0,
          trigger_type: 1,
          wx_alarm: 1,
          qy_alarm: 0,
          sms_alarm: 0,
          voice_alarm: 0,
          //email_alarm:0,
          qy_addr: '',
          sms_contact: '',
          voice_contact: '',
          recover_content: '',
          alarm_content: '',
          payer_id: 0,
          keep_time: '',
          value1: '',
          value2: '',
          equip_id: '',
          slave_id: '',
          data_key: '',
          product_id: '',
          is_forward: 0,
          forwards: []
        },
        triggerTypes: [{
          value: 1,
          label: '数据触发'
        }, {
          value: 2,
          label: '事件触发'
        }],
        whileIfs2: [{
            value: 1,
            label: '上下线'
          }, {
            value: 2,
            label: '上线'
          }, {
            value: 3,
            label: '下线'
          }, {
            value: 4,
            label: '断电'
          },
          {
            value: 5,
            label: '数据上报'
          }
        ],
        showTable: false,
        isFirst: true,
        tableData: [],
        linkData: [],
        linkSlaves: [],
        linkSensors: [],
        onDevices: [],
        onSlaves: [],
        aboutForm: {
          "type": 1,
          "linkage_equip_id": '',
          "linkage_slave_id": '',
          "linkage_sensor_id": '',
          "linkage_value": 1,
          "linkage_recover_value": 0,
        },
        editForm: {
          "linkage_equip_id": '',
          "linkage_slave_id": '',
          "linkage_sensor_id": '',
          "linkage_value": '',
          "linkage_recover_value": '',
          "type": 1
        },
        addForwardForm: {
          forward_equip_id: '',
          forward_equip_code: '',
          forward_equip_name: '', //追加的字段，用以在表格中显示名称
          equip_type: 1,
          forward_template: [{
            slave_id: 0,
            sensors: [],
            slave_no: "",
            //sensor_id:0,
            data_key: "",
            formula: ""
          }]
        },
        editForwardForm: {
          forward_equip_id: '',
          forward_equip_code: '',
          forward_equip_name: '', //追加的字段，用以在表格中显示名称
          equip_type: 1,
          forward_template: [{

          }]
        },
        forwardSlaves: [{
          value: 0,
          label: ''
        }],
        products: [],
        devices: [],
        slaves: [{
          value: 0,
          label: "全部"
        }],
        whileIfs: whileIfs,
        setForm: {
          name: '无'
        },
        children: [],
        childrenFinance: []
      }
    },

    components: {
      FormulaCreater,
      FormulaInfo
    },

    // watch: {
    //    filterText(val) {
    // 	  this.$refs.tree.filter(val);
    //    }
    // },

    created() {
      this.id = localStorage.getItem('editHappenId');
      // if (this.grade <= 2) {
      this.getProduct();
      // } else {
      //  this.getDevice();
      //}
      //this.getChildren();
      var that = this;
      setTimeout(function() {
        that.getInfo(that.id);
      }, 2000);
    },
    methods: {
      getProduct() {
        var that = this;
        this.$http({
          method: 'post',
          url: '/api/product/menuTree',
          data: {}
        }).then(res => {
          if (res && res.data.result == "success") {
            this.products = res.data.data;
          }
          //console.log("charu")
        }).catch(error => {

          console.log(error);
        });
      },

      async handleChange(par) {
        var that = this;
        this.isFirst = false;
        this.onDevices = [];
        this.devices = [];
        this.form.equip_id = '';
        this.form.data_key = '';
        this.form.slave_id = '';
        await this.getDevice(par);
        setTimeout(function() {
          that.form.equip_id = that.devices[0].value;
          if (that.devices[0].type == 1 && that.devices[0].slaveCount > 1) {
            that.showType = true;
            that.getSlave(that.form.equip_id);
            //子设备为全部的时候不请求
            that.form.slave_id = that.slaves[0].value;
            that.form.data_key = "";
          } else {
            that.showType = false;
            that.getSensor(that.form.equip_id);
            setTimeout(function() {
              //console.log('b');
              that.form.data_key = that.dataKeys[0].value;
              that.$forceUpdate();
            }, 1000)
          }
        }, 1000)
        this.showType = false;
        // setTimeout(function(){
        //   that.aboutForm.linkage_equip_id = that.onDevices[0].value;
        //   that.changeOnDevice(that.onDevices[0].value);
        // },800);
      },

      chooseAlarm(type, value) {
        if (type == "wx") {
          this.form.wx_alarm = value == 1 ? 0 : 1;
        } else if (type == "sms") {
          this.form.sms_alarm = value == 1 ? 0 : 1;
        } else if (type == "voice") {
          this.form.voice_alarm = value == 1 ? 0 : 1;
        } else if (type == "qy") {
          this.form.qy_alarm = value == 1 ? 0 : 1;
        }
      },

      getChildren(id) {
        this.children = [];
        this.childrenFinance = [];
        this.$http({
          method: 'post',
          url: 'api/user/contact',
          data: {
            'id': localStorage.getItem("userId"),
          }
        }).then(res => {
          if (res && res.data.result == "success") {
            for (let i = 0, len = res.data.data.length; i < len; i++) {
              // let child={
              // 	value:Number(res.data.data[i].id),
              // 	label:res.data.data[i].user_name
              // }
              // this.children.push(child);
              if (res.data.data[i].allow_finance == 1) {
                //开通了付费的用户
                let child = {
                  value: Number(res.data.data[i].id),
                  label: res.data.data[i].user_name
                }
                this.childrenFinance.push(child);
              }
            }
            //this.form.payer_id = this.childrenFinance.length?this.childrenFinance[0].value:'';
          }
        }).catch(error => {
          console.log(error);

        });
      },
      getInfo(id) {
        var that = this;
        this.tableData = [];
        this.linkData = [];
        this.$http({
          method: 'post',
          url: 'api/trigger/info',
          data: {
            "id": id
          }
        }).then(res => {
          if (res && res.data.result == "success") {
            //this.form = res.data.data;
            for (var key in this.form) {
              this.form[key] = res.data.data[key];
            }
            //如果是上下线触发(trigger_type==2)，恢复报警将禁用
            this.form.recover_alarm = res.data.data.trigger_type == 1 ? res.data.data.recover_alarm : 0;
            this.form.payer_id = res.data.data.payer_id ? Number(res.data.data.payer_id) : '';
            this.getDevice(this.form.product_id);
            if (this.isFirst) {
              setTimeout(function() {
                that.form.equip_id = res.data.data.equip_id;
                for (let n = 0, len = that.devices.length; n < len; n++) {
                  //要设备ID不等于0，既不等于全部，才可执行请求
                  if (that.form.equip_id && (that.form.equip_id == that.devices[n].value)) {
                    if ((that.devices[n].type == 1) && (that.devices[n].slaveCount > 1)) {
                      that.showType = true;
                      that.getSlave(that.form.equip_id);
                      that.form.slave_id = res.data.data.slave_id;
                      //如果子设备为全部（即0）时，不再请求数据点，因为是手填
                      if (Number(that.form.slave_id)) {
                        that.getSensor(that.form.slave_id);
                      }
                    } else {
                      that.showType = false;
                      that.getSensor(res.data.data.equip_id);
                    }
                    that.form.data_key = res.data.data.data_key;
                    break;
                  } else if (!that.form.equid_id) {
                    that.form.data_key = res.data.data.data_key;
                  }
                }
                that.loading = false;
              }, 1000);
            }
            if (res.data.data.is_linkage) {
              this.tableData = res.data.data.linkages;
              for (let i = 0, len = res.data.data.linkages.length; i < len; i++) {
                let about = Object.assign({}, res.data.data.linkages[i]);
                delete about.equip_name;
                delete about.sensor_id;
                delete about.sensor_name;
                delete about.slave_name;
                this.linkData.push(about);
                this.tableData[i].sensor_name = res.data.data.linkages[i].data_key + ' (' + res.data.data.linkages[
                    i].sensor_name +
                  ')';
              }
              //console.log(this.linkData);
            }
            if (res.data.data.is_forward == 1 && res.data.data.forwards.length) {
              for (let i = 0, len = res.data.data.forwards.length; i < len; i++) {
                this.form.forwards[i].forward_equip_id = Number(res.data.data.forwards[i].forward_equip_id);
                this.form.forwards[i].forward_template = Array.from(res.data.data.forwards[i].forward_template);
                for (let n = 0, len2 = res.data.data.forwards[i].forward_template.length; n < len2; n++) {
                  this.form.forwards[i].forward_template[n].slave_id = Number(this.form.forwards[i]
                    .forward_template[n].slave_id);
                  //this.$set(this.form.forwards[i].forward_template[n],'sensors',[]);
                }
              }
            }
            if (!res.data.data.equip_name) {
              this.form.equip_id = this.devices[0].value;
            }
            if (!res.data.data.slave_name) {
              this.form.slave_id = this.slaves[0].value;
            }
          } else {
            this.loading = false;
          }
        }).catch(error => {
          this.loading = false;
          console.log(error);
        });
      },
      goBack() {
        this.$router.go(-1);
      },
      //获取项目下所有设备函数
      getDevice(id) {
        var that = this;
        this.onDevices = [];
        this.devices = [];
        //console.log(id)
        this.$http({
          method: 'post',
          url: 'api/equip/all',
          data: {
            'product_id': id

          }
        }).then(res => {
          if (res && res.data.result == "success") {
            if (res.data.data.length) {
              for (let i = 0, len = res.data.data.length; i < len; i++) {
                let device = {
                  value: res.data.data[i].id,
                  label: res.data.data[i].equip_name,
                  equipCode: res.data.data[i].equip_code,
                  type: res.data.data[i].type,
                  slaveCount: res.data.data[i].slaves_count
                }
                this.devices.push(device);
                //this.onDevices.push(device);
              }
              this.onDevices = Array.from(that.devices);
              this.aboutForm.linkage_equip_id = this.onDevices[0].value;
              this.aboutForm.type = this.onDevices[0].type;

              if (!this.isFirst) {
                setTimeout(function() {
                  if (that.aboutForm.type == 1) {
                    that.getOnSlave(that.onDevices[0].value);
                    setTimeout(function() {
                      that.aboutForm.linkage_slave_id = that.onSlaves[0].value;
                      that.changeOnSlave(that.onSlaves[0].value);
                    }, 500)
                  } else {
                    that.aboutForm.linkage_slave_id = '';
                    that.getOnSensor(that.onDevices[0].value);
                    setTimeout(function() {
                      if (that.onSensors.length) {
                        that.aboutForm.linkage_sensor_id = that.onSensors[0].value;
                      }
                    }, 1000)
                  }

                }, 1000)
              }

            }
            return true;
          }
        }).catch(error => {
          console.log(error)
          return false;
        });
      },

      getSlave(id) {
        var that = this;
        this.slaves = [{
          value: 0,
          label: "全部",
        }];
        this.$http({
          method: 'post',
          url: 'api/slave/all',
          data: {
            "equip_id": id

          }
        }).then(res => {
          if (res && res.data.result == "success") {
            if (res.data.data.length) {
              for (let i = 0; i < res.data.data.length; i++) {
                let slave = {
                  value: res.data.data[i].id,
                  label: res.data.data[i].slave_name
                }
                that.slaves.push(slave)
              }
            } else {
              this.form.slave_id = this.slaves[0].value;
            }
          }
        }).catch(error => {
          console.log(error);
        });
      },

      getOnSlave(id) {
        var that = this;
        this.onSlaves = [];
        this.$http({
          method: 'post',
          url: 'api/slave/all',
          data: {
            "equip_id": id

          }
        }).then(res => {
          if (res && res.data.result == "success") {
            for (let i = 0; i < res.data.data.length; i++) {
              let onslave = {
                value: res.data.data[i].id,
                label: res.data.data[i].slave_name
              }
              that.onSlaves.push(onslave)
            }
            //that.aboutForm.slave_name = that.onSlaves[0].value;

          }
          // console.log(that.pointdata[0].icon)
        }).catch(error => {
          console.log(error);

        });
      },

      getSensor(id) {
        var that = this;
        this.dataKeys = [];
        this.$http({
          method: 'post',
          url: 'api/sensor/all',
          data: that.showType ? {
            "slave_id": id

          } : {
            "equip_id": id

          }
        }).then(res => {
          if (res && res.data.result == "success") {
            for (let i = 0, len = res.data.data.length; i < len; i++) {
              let dataKey = {
                value: res.data.data[i].data_key,
                label: res.data.data[i].data_key + ' (' + res.data.data[i].sensor_name + ')'
              }
              this.dataKeys.push(dataKey);
            }
          }

        }).catch(error => {
          console.log(error)
        });

      },

      getOnSensor(id) {
        var that = this;
        this.onSensors = [];
        this.$http({
          method: 'post',
          url: 'api/sensor/all',
          data: this.aboutForm.type == 1 ? {
            "slave_id": id

          } : {
            "equip_id": id

          }
        }).then(res => {
          if (res && res.data.result == "success") {
            for (let i = 0, len = res.data.data.length; i < len; i++) {
              if (res.data.data[i].read_write == 2) {
                let onSensor = {
                  value: res.data.data[i].id,
                  label: res.data.data[i].data_key + ' (' + res.data.data[i].sensor_name + ')'
                }
                that.onSensors.push(onSensor);
              }
            }
          }

        }).catch(error => {
          console.log(error)
        });

      },

      changeTriggerType(type) {
        if (type == 2) {
          this.form.recover_alarm = 0;
        }
      },

      changeSend(send) {
        if (!send) {
          this.form.recover_alarm = 0;
        }
      },

      changeAlarm(isAlarm) {
        if (!isAlarm) {
          this.form.is_send = 0;
          this.form.recover_alarm = 0;
        }
      },

      changeDevice(device) {
        var that = this;
        this.isFirst = false;
        this.dataKeys = [];
        if (Number(device)) {
          for (let i = 0; i < this.devices.length; i++) {
            if (device == this.devices[i].value) {
              if (this.devices[i].type == 1 && this.devices[i].slaveCount > 1) {
                this.showType = true;
                this.getSlave(device);
                //子设备为全部的时候不请求
                this.form.slave_id = this.slaves[0].value;
                this.form.data_key = "";
              } else {
                this.showType = false;
                this.getSensor(this.form.equip_id);
                setTimeout(function() {
                  //console.log('b');
                  that.form.data_key = that.dataKeys[0].value;
                  that.$forceUpdate();
                }, 1000)
              }
              break;
            }
          }
        } else {
          this.showType = false;
          this.form.is_forward = 0;
          this.form.is_linkage = 0;
          this.form.data_key = '';
        }
      },

      preAddLink() {
        this.addFormVisible = true;
        this.aboutForm.linkage_equip_id = this.onDevices[0].value;
        this.changeOnDevice(this.aboutForm.linkage_equip_id);
      },

      addLink() {
        if (!this.aboutForm.linkage_value && (!this.aboutForm.linkage_recover_value)) {
          this.$message({
            message: '触发下发值和恢复下发值不能同时为空!',
            type: 'error',
            center: true
          });
          return false;
        }
        //引用型数据不能等号传值，会相互影响，要拷贝；
        function copy(obj) {
          return Object.assign({}, obj);
          //return {...obj};//也可以用扩展运算符
        };
        var about = copy(this.aboutForm);
        delete about.slaveCount;
        delete about.type;
        this.linkData.push(about);
        var change = {
          "equip_name": '',
          "slave_name": '',
          "sensor_name": '',
          "linkage_value": this.aboutForm.linkage_value,
          "linkage_recover_value": this.aboutForm.linkage_recover_value,
        };
        for (let i = 0, len = this.onDevices.length; i < len; i++) {
          if (this.onDevices[i].value == this.aboutForm.linkage_equip_id) {
            change.equip_name = this.onDevices[i].label;
            break;
          }
        }
        if (this.onSlaves[0]) {
          for (let i = 0, len = this.onSlaves.length; i < len; i++) {
            if (this.onSlaves[i].value == this.aboutForm.linkage_slave_id) {
              change.slave_name = this.onSlaves[i].label;
              break;
            }
          }

        }
        for (let i = 0, len = this.onSensors.length; i < len; i++) {
          if (this.onSensors[i].value == this.aboutForm.linkage_sensor_id) {
            change.sensor_name = this.onSensors[i].label;
            break;
          }
        }
        this.tableData.push(change);
        this.closeAddDialog();
      },

      async linkEdit(row, index) {
        var that = this;
        this.editIndex = index;
        this.editFormVisible = true;
        //console.log(this.linkData);
        this.editForm.linkage_value = row.linkage_value;
        this.editForm.linkage_recover_value = row.linkage_recover_value;
        //delete删除对象属性，Vue中用下面this.$delete的方法删除对象属性；
        //this.$delete(this.editForm,'linkage_value');
        //console.log(this.editForm.linkage_value);
        for (let i = 0, len = this.onDevices.length; i < len; i++) {
          if (this.onDevices[i].label == row.equip_name) {
            this.editForm.linkage_equip_id = this.onDevices[i].value;
            this.editForm.type = this.onDevices[i].type;
            if (this.editForm.type == 1) {
              await this.getLinkSlave(this.onDevices[i].value);
              setTimeout(function() {
                for (let n = 0, nlen = that.linkSlaves.length; n < nlen; n++) {
                  if (that.linkSlaves[n].label == row.slave_name) {
                    that.editForm.linkage_slave_id = that.linkSlaves[n].value;
                    that.getLinkSensor(that.linkSlaves[n].value);
                    setTimeout(function() {
                      for (let m = 0, mlen = that.linkSensors.length; m < mlen; m++) {
                        if (that.linkSensors[m].label == row.sensor_name) {
                          that.editForm.linkage_sensor_id = that.linkSensors[m].value;
                          break;
                        }
                      }
                    }, 1000)
                    break;
                  }
                }
              }, 500);
            } else {
              await this.getLinkSensor(this.onDevices[i].value);
              setTimeout(function() {
                for (let m = 0, mlen = that.linkSensors.length; m < mlen; m++) {
                  if (that.linkSensors[m].label == row.sensor_name) {
                    that.editForm.linkage_sensor_id = that.linkSensors[m].value;
                    break;
                  }
                }
              }, 500)

            }
            break;
          }
        }

      },

      sureEdit() {
        var that = this;
        if (!this.editForm.linkage_value && (!this.editForm.linkage_recover_value)) {
          this.$message({
            message: '触发下发值和恢复下发值不能同时为空!',
            type: 'error',
            center: true
          });
          return false;
        }
        var about = Object.assign({}, that.editForm);
        delete about.type;
        delete about.slaveCount;
        this.linkData[this.editIndex] = about;
        //console.log(this.linkData);
        this.tableData[that.editIndex].linkage_value = this.editForm.linkage_value;
        this.tableData[that.editIndex].linkage_recover_value = this.editForm.linkage_recover_value;
        for (let i = 0, len = this.onDevices.length; i < len; i++) {
          if (this.onDevices[i].value == this.editForm.linkage_equip_id) {
            this.tableData[that.editIndex].equip_name = this.onDevices[i].label;
            break;
          }
        }
        if (this.linkSlaves[0]) {
          for (let i = 0, len = this.linkSlaves.length; i < len; i++) {
            if (this.linkSlaves[i].value == this.editForm.linkage_slave_id) {
              this.tableData[that.editIndex].slave_name = this.linkSlaves[i].label;
              break;
            }
          }
        }
        for (let i = 0, len = this.linkSensors.length; i < len; i++) {
          if (this.linkSensors[i].value == this.editForm.linkage_sensor_id) {
            this.tableData[that.editIndex].sensor_name = this.linkSensors[i].label;
            break;
          }
        }
        this.editFormVisible = false;
      },

      cancelEdit() {
        this.editFormVisible = false;
        this.editForm = {
          linkage_equip_id: '',
          linkage_slave_id: '',
          linkage_sensor_id: '',
          linkage_value: '',
          linkage_recover_value: '',
          type: 1,
          slaveCount: 1
        };
      },

      async changeEditSlave(slave) {
        var that = this;
        await this.getLinkSensor(slave);
        setTimeout(function() {
          that.editForm.linkage_sensor_id = that.linkSensors[0].value;
        }, 500)
      },

      async changeEditDevice(device) {
        var that = this;
        for (let i = 0, len = this.onDevices.length; i < len; i++) {
          if (this.onDevices[i].value == this.editForm.linkage_equip_id) {
            this.editForm.type = this.onDevices[i].type;
            if (this.editForm.type == 1) {
              await this.getLinkSlave(device);
              setTimeout(function() {
                that.editForm.linkage_slave_id = that.linkSlaves[0].value;
                that.getLinkSensor(that.linkSlaves[0].value);
                setTimeout(function() {
                  that.editForm.linkage_sensor_id = that.linkSensors[0].value;
                }, 1000)
              }, 500);
              break;
            } else {
              await this.getLinkSensor(device);
              setTimeout(function() {
                that.editForm.linkage_sensor_id = that.linkSensors[0].value;
              }, 500);
              break;
            }
          }
        }

      },

      closeAddDialog() {
        this.addFormVisible = false;
        this.aboutForm.linkage_equip_id = this.onDevices[0].value;
        this.changeOnDevice(this.onDevices[0].value);
        this.aboutForm.linkage_value = 1;
        this.aboutForm.linkage_recover_value = 0;
      },

      getLinkSlave(id) {
        var that = this;
        this.linkSlaves = [];
        this.$http({
          method: 'post',
          url: 'api/slave/all',
          data: {
            "equip_id": id

          }
        }).then(res => {
          if (res && res.data.result == "success") {
            for (let i = 0; i < res.data.data.length; i++) {
              let onslave = {
                value: res.data.data[i].id,
                label: res.data.data[i].slave_name
              }
              that.linkSlaves.push(onslave)
            }
          }
          // console.log(that.pointdata[0].icon)
        }).catch(error => {
          console.log(error);

        });
      },

      getLinkSensor(id) {
        var that = this;
        this.linkSensors = [];
        this.$http({
          method: 'post',
          url: 'api/sensor/all',
          data: this.editForm.type == 1 ? {
            "slave_id": id

          } : {
            "equip_id": id

          }
        }).then(res => {
          if (res && res.data.result == "success") {
            for (let i = 0, len = res.data.data.length; i < len; i++) {
              if (res.data.data[i].read_write == 2) {
                let onSensor = {
                  value: res.data.data[i].id,
                  label: res.data.data[i].data_key + ' (' + res.data.data[i].sensor_name + ')'
                }
                this.linkSensors.push(onSensor);
              }
            }
          }

        }).catch(error => {
          console.log(error)
        });

      },

      sureDelete(row) {
        var index = this.tableData.indexOf(row);
        var arr = [];
        if (index != -1) {
          this.tableData.splice(index, 1);
          this.linkData.splice(index, 1);
        }
        //console.log(this.linkData);
      },

      changeSlave(slave) {
        var that = this;
        that.form.data_key = '';
        if (slave) {
          this.getSensor(this.form.slave_id);
          setTimeout(function() {
            if (that.dataKeys.length) {
              that.form.data_key = that.dataKeys[0].value;
            } else {
              that.form.data_key = '';
            }
            that.$forceUpdate();
          }, 1000);
        } else {
          this.form.data_key = '';
        }
        that.$forceUpdate();
      },

      async changeOnSlave(slave) {
        var that = this;
        if (slave) {
          await this.getOnSensor(slave)
          setTimeout(function() {
            if (that.onSensors.length) {
              that.aboutForm.linkage_sensor_id = that.onSensors[0].value;
            }
          }, 500)
        }
      },



      async changeOnDevice(device) {
        this.onSlaves = [];
        this.onSensors = [];
        this.aboutForm.linkage_slave_id = '';
        this.aboutForm.linkage_sensor_id = '';
        //that.aboutForm.slave_name='';
        //that.aboutForm.sensor_name='';
        var that = this;
        for (var i = 0; i < this.onDevices.length; i++) {
          if (this.onDevices[i].value == device) {
            this.aboutForm.type = this.onDevices[i].type;
            break;
          }
        }
        // setTimeout(function() {
        if (that.aboutForm.type == 1) {
          await that.getOnSlave(device);
          setTimeout(function() {
            that.changeOnSlave(that.onSlaves[0].value);
            that.aboutForm.linkage_slave_id = that.onSlaves[0].value;
          }, 500)
        } else {
          that.aboutForm.linkage_slave_id = '';
          await that.getOnSensor(device);
          setTimeout(function() {
            if (that.onSensors.length) {
              that.aboutForm.linkage_sensor_id = that.onSensors[0].value;
            }
          }, 500)
        }

        // }, 1000)
      },


      changeCondition(cond) {
        if (cond == 5) {
          this.form.sync_alarm = 0;
          this.form.is_linkage = 0;
          this.form.is_send = 0;
          this.form.recover_alarm = 0;
          this.form.is_forward = this.form.equip_id != 0 ? 1 : 0;
        } else if (this.form.trigger_type == 2 && cond != 5) {
          this.form.is_forward = 0;
        }
      },

      async addForward() {
        var that = this;
        this.addForwardVisible = true;
        this.drawerShow = false;
        this.isCopy = false;
        this.sensorString = '';
        //不能自己给自己转发，所以要排除掉
        this.forwardDevices = await this.onDevices.filter(function(item, index, arr) {
          return item.value != that.form.equip_id;
        });
        this.addForwardForm.forward_equip_id = this.forwardDevices[0].value;
        this.addForwardForm.forward_equip_code = this.forwardDevices[0].equipCode;
        this.addForwardForm.forward_equip_name = this.forwardDevices[0].label;
        this.addForwardForm.equip_type = this.forwardDevices[0].type; //1是网关，2是直连
        this.addForwardForm.forward_template = [{
          slave_id: '',
          slave_no: '',
          sensors: [],
          //sensor_id:'',
          data_key: '',
          formula: ''
        }];
        if (this.addForwardForm.equip_type == 1) {
          this.getForwardSlave(this.forwardDevices[0].value, 0, 'add');
        } else {
          this.getForwardSensor(this.forwardDevices[0].value, 0, 'add');
        }
        this.getSlaveSensorTree(this.form.equip_id);
        setTimeout(function() {
          that.drawerShow = true;
        }, 500)
      },

      varStringHelp() {
        this.drawerShow = !this.drawerShow;
      },

      //获取设备的从机变量树
      getSlaveSensorTree(id) {
        var that = this;
        this.sensorTree = [];
        this.$http({
          method: 'post',
          url: 'api/equip/sensorTree', //该接口已去掉虚拟变量
          data: {
            "id": id,
          }
        }).then(res => {
          if (res && res.data.result == "success") {
            if (res.data.data.length) {
              //console.log(res.data.data);
              this.sensorTree = Array.from(res.data.data);
              this.$forceUpdate();
            }
          }
        }).catch(error => {
          console.log(error);
        });
      },

      // filterNode(value, data) {
      // 	if (!value) return true;
      // 	return data.label.indexOf(value) !== -1;
      // },

      // getSensorString(data,node,ele){
      // 	//console.log(data);
      // 	if(!data.data_key){
      // 		return
      // 	}
      // 	this.sibling=ele.$el.parentNode.children;
      // 	// for(let i=0;i<this.sibling.length;i++){
      // 	// 	  this.sibling[i].style.color="#333";
      // 	// }
      // 	ele.$el.style.color = "#409eff";
      // 	let str='$devices["'+data.slave_no+'"]["variables"]["'+data.data_key+'"]'+'  ';
      // 	this.sensorString=this.sensorString + str;
      // },

      // reSetString(){
      // 	this.sensorString="";
      // 	for(let i=0;i<this.sibling.length;i++){
      // 		this.sibling[i].style.color="#333";
      // 	}
      // },
      //丛子组件中获取变量地址字符串
      getSensorString(str) {
        this.sensorString = str;
        //console.log(this.sensorString);
      },

      fillString(item) {
        if (this.sensorString) {
          item.formula = this.sensorString.trim();
        }
      },

      // async copyString() {
      // 	var that=this;
      // 	await this.$refs.sensorString.select();
      // 	document.execCommand('copy');
      // 	this.isCopy=true;
      // 	setTimeout(function(){
      // 		that.isCopy=false;
      // 	},5000)
      // 	console.log('复制成功');
      // },

      changeForwardEquip(id, oper) {
        if (oper == 'add') {
          if (this.addForwardForm.forward_template.length > 1) {
            this.addForwardForm.forward_template = [{
              slave_id: 0,
              sensors: [],
              slave_no: "",
              //sensor_id:0,
              data_key: "",
              formula: ""
            }];
          } else {
            let formula = this.addForwardForm.forward_template[0].formula
            this.addForwardForm.forward_template = [{
              slave_id: 0,
              sensors: [],
              slave_no: "",
              //sensor_id:0,
              data_key: "",
              formula: formula
            }];
          }
        } else {
          if (this.editForwardForm.forward_template.length > 1) {
            this.editForwardForm.forward_template = [{
              slave_id: 0,
              sensors: [],
              slave_no: "",
              //sensor_id:0,
              data_key: "",
              formula: ''
              //formula:formula
            }];
          } else {
            let formula = this.editForwardForm.forward_template[0].formula;
            this.editForwardForm.forward_template = [{
              slave_id: 0,
              sensors: [],
              slave_no: "",
              //sensor_id:0,
              data_key: "",
              formula: '',
              formula: formula
            }];
          }
        }

        for (let i = 0, len = this.forwardDevices.length; i < len; i++) {
          if (this.forwardDevices[i].value == id) {
            if (oper == 'add') {
              this.addForwardForm.equip_type = this.forwardDevices[i].type;
              this.addForwardForm.forward_equip_id = this.forwardDevices[i].value;
              this.addForwardForm.forward_equip_code = this.forwardDevices[i].equipCode;
              this.addForwardForm.forward_equip_name = this.forwardDevices[i].label;
              if (this.addForwardForm.equip_type == 1) {
                this.getForwardSlave(id, 0, 'add');
              } else {
                this.getForwardSensor(id, 0, 'add');
              }
            } else {
              this.editForwardForm.equip_type = this.forwardDevices[i].type;
              this.editForwardForm.forward_equip_code = this.forwardDevices[i].equipCode;
              this.editForwardForm.forward_equip_name = this.forwardDevices[i].label;
              if (this.editForwardForm.equip_type == 1) {
                this.getForwardSlave(id, 0, 'edit');
              } else {
                this.getForwardSensor(id, 0, 'edit');
              }
            }
            break;
          }
        }

      },

      getForwardSlave(id, index, oper) {
        var that = this;
        this.forwardSlaves = [];
        this.$http({
          method: 'post',
          url: 'api/slave/all',
          data: {
            "equip_id": id
          }
        }).then(res => {
          if (res && res.data.result == "success") {
            for (let i = 0; i < res.data.data.length; i++) {
              let slave = {
                value: res.data.data[i].id,
                slaveNo: res.data.data[i].slave_no,
                label: res.data.data[i].slave_name
              }
              this.forwardSlaves.push(slave);
            }
            if (oper == 'add') {
              for (let i = 0, len = this.addForwardForm.forward_template.length; i < len; i++) {
                this.getForwardSensor(this.forwardSlaves[0].value, i, 'add');
              }
            } else {
              for (let i = 0, len = this.editForwardForm.forward_template.length; i < len; i++) {
                if (this.editForwardForm.forward_template[i].slave_id) {
                  this.getForwardSensor(this.editForwardForm.forward_template[i].slave_id, i, 'edit');
                } else {
                  this.getForwardSensor(this.forwardSlaves[0].value, i, 'edit');
                }
              }
            }
          }
        }).catch(error => {
          console.log(error);
        });

      },

      changeForwardSlave(id, index, oper) {
        this.getForwardSensor(id, index, oper);
      },

      getForwardSensor(id, index, oper) {
        var that = this;
        var sensors = [];
        if (oper == "add") {
          this.addForwardForm.forward_template[index].sensors = [];
        } else {
          this.editForwardForm.forward_template[index].sensors = [];
        }
        this.$http({
          method: 'post',
          url: 'api/sensor/all',
          data: (oper == 'add' && this.addForwardForm.equip_type == 1) || (oper == 'edit' && this.editForwardForm
            .equip_type == 1) ? {
            "slave_id": id

          } : {
            "equip_id": id

          }
        }).then(res => {
          if (res && res.data.result == "success") {
            for (let i = 0, len = res.data.data.length; i < len; i++) {
              let sensor = {
                //value: res.data.data[i].id,
                value: res.data.data[i].data_key,
                //label: res.data.data[i].data_key + ' (' + res.data.data[i].sensor_name + ')'
                label: res.data.data[i].sensor_name
              }
              sensors.push(sensor);
            }
            if (res.data.data.length) {
              if (oper == 'add') {
                if (this.addForwardForm.equip_type == 1) {
                  this.addForwardForm.forward_template[index].slave_id = Number(id);
                  this.addForwardForm.forward_template[index].sensors = Array.from(sensors);
                  this.addForwardForm.forward_template[index].data_key = sensors[0].value;
                  for (let n = 0, len = this.forwardSlaves.length; n < len; n++) {
                    if (id == this.forwardSlaves[n].value) {
                      this.addForwardForm.forward_template[index].slave_no = this.forwardSlaves[n].slaveNo;
                      break;
                    }
                  }
                }
              } else {
                this.editForwardForm.forward_template[index].sensors = Array.from(sensors);
                if (!this.editForwardForm.forward_template[index].data_key) {
                  this.editForwardForm.forward_template[index].data_key = sensors[0].value;
                }
                if (this.editForwardForm.equip_type == 1) {
                  this.editForwardForm.forward_template[index].slave_id = id;
                  for (let i = 0, len = this.forwardSlaves.length; i < len; i++) {
                    if (id == this.forwardSlaves[i].value) {
                      this.editForwardForm.forward_template[index].slave_no = this.forwardSlaves[i].slaveNo;
                      break;
                    }
                  }
                }

              }
            } else {
              this.editForwardForm.forward_template[index].sensors = [];
              this.editForwardForm.forward_template[index].data_key = "";
              this.addForwardForm.forward_template[index].sensors = [];
              this.addForwardForm.forward_template[index].data_key = "";
            }
          }

        }).catch(error => {
          console.log(error)
        });
      },

      sureAddForward() {
        var that = this;
        var hasEmpty = this.addForwardForm.forward_template.some(function(item, index, arr) {
          return item.formula == '';
        });
        if (hasEmpty) {
          this.$message({
            message: '计算公式不能为空!',
            type: 'error',
            center: true
          })
          return
        }
        var newForward = Object.assign({}, that.addForwardForm);
        this.form.forwards.push(newForward);
        this.addForwardVisible = false;
      },

      addDomain(oper) {
        var that = this;
        var len = 1;
        if (oper == "add") {
          len = this.addForwardForm.forward_template.length;
          this.addForwardForm.forward_template.push({
            slave_id: this.addForwardForm.equip_type == 1 ? that.forwardSlaves[0].value : '',
            slave_no: this.addForwardForm.equip_type == 1 ? that.forwardSlaves[0].slaveNo : '',
            sensors: [],
            //sensor_id:'',
            data_key: '',
            formula: ''
          });

          if (this.addForwardForm.equip_type == 1) {
            //console.log(this.addForwardForm.forward_template.length);
            this.changeForwardSlave(this.forwardSlaves[0].value, len, 'add');
          } else {
            //console.log(this.addForwardForm.forward_template.length);
            this.getForwardSensor(this.addForwardForm.forward_equip_id, len, 'add');
          }
        } else if (oper == 'edit') {
          len = this.editForwardForm.forward_template.length;
          this.editForwardForm.forward_template.push({
            slave_id: this.editForwardForm.equip_type == 1 ? that.forwardSlaves[0].value : '',
            slave_no: this.editForwardForm.equip_type == 1 ? that.forwardSlaves[0].slaveNo : '',
            sensors: [],
            //sensor_id:'',
            data_key: '',
            formula: ''
          });

          if (this.editForwardForm.equip_type == 1) {
            //console.log(this.addForwardForm.forward_template.length);
            this.changeForwardSlave(this.forwardSlaves[0].value, len, 'edit');
          } else {
            //console.log(this.addForwardForm.forward_template.length);
            this.getForwardSensor(this.editForwardForm.forward_equip_id, len, 'edit');
          }
        }
      },

      removeDomain(item, oper) {
        if (oper == 'add') {
          var index = this.addForwardForm.forward_template.indexOf(item)
          if (index !== -1) {
            this.addForwardForm.forward_template.splice(index, 1)
          }
        } else {
          var index = this.editForwardForm.forward_template.indexOf(item)
          if (index !== -1) {
            this.editForwardForm.forward_template.splice(index, 1)
          }
        }
      },

      showForwardInfo(row, index) {
        this.forwardTemplateInfo = Object.assign({}, row);
        this.forwardTemplateVisible = true;
      },

      async forwardEdit(row, index) {
        var that = this;
        //不能自己给自己转发，所以要排除掉
        this.forwardDevices = await this.onDevices.filter(function(item, index, arr) {
          return item.value != that.form.equip_id;
        });
        this.drawerShow = false;
        this.sensorString = "";
        this.editForwardLoading = true;
        this.editForwardIndex = index;
        this.editForwardForm = Object.assign({}, row);
        this.editForwardForm.equip_type = row.equip_type;
        if (this.editForwardForm.equip_type == 1) {
          this.getForwardSlave(this.editForwardForm.forward_equip_id, index, 'edit');
        } else {
          for (let i = 0, len = this.editForwardForm.forward_template.length; i < len; i++) {
            this.getForwardSensor(this.editForwardForm.forward_equip_id, i, 'edit');
          }
        }
        this.getSlaveSensorTree(this.form.equip_id);
        setTimeout(function() {
          that.drawerShow = true;
        }, 500)
        this.editForwardVisible = true;
        setTimeout(function() {
          that.editForwardLoading = false;
        }, 2000)
      },

      sureEditForward() {
        var that = this;
        var hasEmpty = this.editForwardForm.forward_template.some(function(item, index, arr) {
          return item.formula == '';
        });
        if (hasEmpty) {
          this.$message({
            message: '计算公式不能为空!',
            type: 'error',
            center: true
          })
          return
        }
        this.form.forwards[that.editForwardIndex] = Object.assign({}, that.editForwardForm);
        this.editForwardVisible = false;
        // for(let i=0,len=this.forwardDevices.length;i<len;i++){
        // 	if(this.editForwardForm.forward_equip_id==this.forwardDevices[i].value){
        // 		this.editForwardForm.forward_equip_name=this.forwardDevices[i].label;
        // 		this.form.forwards[this.editForwardIndex]=this.editForwardForm;
        // 		this.editForwardVisible = false;
        // 		break;
        // 	}
        // }
      },

      sureDeleteForward(row) {
        var index = this.form.forwards.indexOf(row);
        //var arr = [];
        if (index != -1) {
          this.form.forwards.splice(index, 1);
        }
      },

      cancelAddForward() {
        this.addForwardVisible = false;
      },

      cancelEditForward() {
        this.editForwardVisible = false;
      },

      forwardInfo() {
        this.forwardInfoVisible = true;
      },

      sureUpdate() {
        var that = this;
        this.form.id = this.id;
        this.form.linkages = Array.from(this.linkData);
        if (this.form.forwards && this.form.forwards.length) {
          for (let i = 0, len = this.form.forwards.length; i < len; i++) {
            for (let n = 0, len2 = this.form.forwards[i].forward_template.length; n < len2; n++) {
              delete this.form.forwards[i].forward_template[n].sensors;
            }
          }
        }
        if (this.form.trigger_type == 2) {
          this.form.recover_alarm = "";
        }
        this.$http({
          method: 'post',
          url: 'api/trigger/update',
          data: this.form
        }).then(res => {
          if (res && res.data.result == "success") {
            this.$message({
              message: '恭喜,修改触发器成功!',
              type: 'success',
              center: true
            });
            this.$router.go(-1);
          }

        }).catch(error => {
          console.log(error)
        });
      }
    }
  }
</script>

<style scoped>
  @media all and (max-width:2560px) {
    .demo {
      margin-left: -1.2rem !important;
      margin-top: 0.2rem !important;
    }

    .tip2 {
      width: 65% !important;
    }
  }

  @media all and (max-width:1920px) {
    .demo {
      margin-left: -1.2rem !important;
    }

    .tip2 {
      width: 60% !important;
    }
  }

  @media all and (max-width:1600px) {
    .demo {
      margin-left: -1.4rem !important;
    }

    .add {
      bottom: 0.1rem !important;
    }

    .tip2 {
      width: 50% !important;
    }
  }

  @media all and (max-width:1440px) {
    .demo {
      margin-left: -1.5rem !important;
    }

    .tip2 {
      width: 48% !important;
      right: 0.5rem !important;
    }
  }

  @media all and (max-width:1366px) {
    .demo {
      margin-left: -1.9rem !important;
      margin-top: 0.3rem !important;
    }

    .tip2 {
      width: 43% !important;
      right: 0.5rem !important;
    }
  }

  #addform {
    text-align: left;
    width: 99.4%;
    /* float: left; */
    line-height: 0.7rem !important;
    /* height:14rem; */
    margin-top: 0.2rem;
  }

  .inpu {
    display: inline-block;
    vertical-align: middle;
    width: 4.5rem;
    font-size: 0.3rem;
    /* font-size: 16px; */
  }

  .tip {
    color: #FF5722;
    font-size: 0.25rem;
    width: 4.6rem;
    margin-top: 0.2rem;
    height: 0.8rem;
    line-height: 0.4rem;
    margin-bottom: 0rem;
  }

  .line {
    width: 100%;
    position: relative;
    height: auto;
    margin-top: 0.1rem;
    padding-top: 0.3rem;
    padding-bottom: 0;
    /* margin-top:-0.4rem;
		margin-bottom:0.5rem; */
    border: 1px dashed #ddd;
  }

  .leftName {
    position: absolute;
    top: -1px;
    left: -1px;
    width: auto;
    padding: 0 0.1rem;
    display: inline-block;
    height: 0.6rem;
    line-height: 0.6rem;
    font-size: 0.3rem;
    background-color: #222831;
    /*#22567b*/
    color: #fff;
    /* border-top-right-radius:50%;
		border-bottom-right-radius:50%; */
  }

  .tip2 {
    width: 50%;
    position: absolute;
    top: 0;
    right: 1rem;
    height: 1.4rem;
    line-height: 0.35rem;
  }

  .add {
    /* position: absolute;
        left: 80px;
        bottom: 0.2rem; */
    font-size: 0.3rem;
    color: #409Eff;
    border: 1px solid #409Eff;
  }

  #notice {
    position: absolute;
    right: 20px;
    top: 30px;
    height: 0.6rem;
    line-height: 0.6rem;
    width: auto;
    font-size: 0.3rem;
    color: red;
  }

  #addform /deep/ .el-form-item {
    margin-bottom: 10px;
  }

  .typeBox {
    display: inline-block;
    width: 0.5rem;
    height: 0.5rem !important;
    line-height: 0.5rem;
    text-align: center;
    transform: translateY(5px);
    margin: 0;
    padding-top: 1px;
    cursor: pointer;
    border-radius: 4px;

  }

  .isChoose {
    border: 1px solid #409eff;
    color: #fff;
    background-color: #409eff;
  }

  .onChoose {
    border: 1px solid #ccc;
    color: #fff;
    background-color: #fff;
  }

  .inpu /deep/ .el-input__inner {
    padding-left: 4px;
    padding-right: 4px;
  }

  .inpu /deep/ .el-textarea__inner {
    padding-left: 4px;
    padding-right: 4px;
  }

  .sortIcon {
    display: inline-block;
    vertical-align: middle;
    margin-left: 0.1rem;
    width: 0.3rem !important;
    height: 0.3rem;
    font-size: 0.25rem;
    line-height: 0.3rem;
    text-align: center;
    color: #999;
    background-color: #ccc;
  }

  .infocontent {
    text-align: left;
    line-height: 0.42rem;
  }

  h4 {
    font-size: 0.3rem;
    color: #111;
    font-weight: 700;
    margin-top: 0.2rem;
  }

  .sensorHead {
    height: 0.6rem;
    line-height: 0.6rem;
    text-align: left;
    /* border:1px solid red; */
    width: 99.6%;
    border-bottom: 1px solid #111;
    margin: 0 0 0.2rem 0;
    padding: 0;
    padding-left: 0.2rem;
  }

  .sensorHead .inpu {
    text-align: center;
    font-size: 0.3rem;
    margin-right: 0.1rem;
  }

  .oneDomain {
    margin-left: 0.2rem;
    height: 0.6rem;
    line-height: 0.6rem;
    width: 98%;
    white-space: nowrap;
  }

  .oneDomain .inpu {
    margin-right: 0.1rem
  }

  .datadial>>>.el-dialog__header {
    position: relative;
    z-index: 99;
    width: 85%;
    height: 0.8rem;
    background-color: transparent !important;
  }

  .datadial>>>.el-dialog__body {
    padding-bottom: 10px;
    padding-top: 10px;
  }
</style>
